import { 战利品, SuppliesData, ItemData } from "./Datas/Data";
import { ItemType, SuppliesType } from "./Framework/Const/Constant";
import { BundleManager, Bundles } from "./Framework/Managers/BundleManager";
import { DataManager } from "./Framework/Managers/DataManager";
import NodeUtil from "./Framework/Utils/NodeUtil";
import { ResourceUtil } from "./Framework/Utils/ResourceUtil";
import { ZTool } from "./Framework/Utils/ZTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Supplies extends cc.Component {

    @property
    generateBox: boolean = false;

    Icon: cc.Sprite = null;
    collider: cc.PhysicsBoxCollider = null;

    data: SuppliesData = null;

    taken: boolean = false;//已经被拿过

    itemDatas = [];

    onLoad() {
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.collider = this.node.getComponent(cc.PhysicsBoxCollider);
    }

    start() {
        if (this.generateBox) {
            this.Init();
        }
    }

    //自然生成的箱子
    Init() {
        let keys = Object.keys(SuppliesType).map(key => SuppliesType[key]).filter(value => typeof value === 'number');
        let type = keys[ZTool.GetRandomInt(0, keys.length)];

        if (type == SuppliesType.None) {
            this.collider.enabled = false;
            return;
        }

        this.itemDatas = [];
        let datas = DataManager.SuppliesDatas.get(type);
        this.data = ZTool.DeepCopy(datas[ZTool.GetRandomInt(0, datas.length)]);

        // BundleManager.LoadSpriteFrame(Bundles.Supplies, `${SuppliesType[data.Type]}_${data.Name}_0`).then((sf: cc.SpriteFrame) => {
        //     this.Icon.spriteFrame = sf;
        // })

        ResourceUtil.LoadSpriteFrame(`Supplies/${this.data.Type}_${this.data.Name}_0`).then((sf: cc.SpriteFrame) => {
            this.Icon.spriteFrame = sf;
        });

        let outGoods = SuppliesData.Out[type];

        let data: 战利品;
        let count: number = 0;

        switch (type) {
            case SuppliesType.医疗用品:
                count = ZTool.GetRandomInt(3, 10);
                for (let i = 0; i < count; i++) {
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.药品)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.药品).length)]);
                    data && (this.itemDatas.push(data));
                }
                break;

            case SuppliesType.弹药箱:
                count = ZTool.GetRandomInt(3, 10);
                for (let i = 0; i < count; i++) {
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.子弹)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.子弹).length)]);
                    data && (this.itemDatas.push(data));
                }
                break;

            case SuppliesType.武器箱:
                //枪
                let index = ZTool.GetRandomInt(0, 7);
                if (index == 0) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.冲锋枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.冲锋枪).length)]);
                if (index == 1) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.卡宾枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.卡宾枪).length)]);
                if (index == 2) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.射手步枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.射手步枪).length)]);
                if (index == 3) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.手枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.手枪).length)]);
                if (index == 4) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.栓动步枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.栓动步枪).length)]);
                if (index == 5) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.突击步枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.突击步枪).length)]);
                if (index == 6) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.轻机枪)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.轻机枪).length)]);
                data && (this.itemDatas.push(data));
                //头盔
                index = ZTool.GetRandomInt(0, 2);
                if (index == 0) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.头盔)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.头盔).length)]);
                if (index == 1) data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.防弹衣)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.防弹衣).length)]);
                data && (this.itemDatas.push(data));
                //子弹
                count = ZTool.GetRandomInt(1, 5);
                for (let i = 0; i < count; i++) {
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.子弹)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(ItemType.子弹).length)]);
                    data && (this.itemDatas.push(data));
                }
                break;
            case SuppliesType.保险柜:
                for (let i = 0; i < this.data.Count; i++) {
                    let goodName = outGoods[ZTool.GetRandomInt(0, outGoods.length)];
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.战利品).find(e => e.Name == goodName));
                    data && (this.itemDatas.push(data));
                }
                break;

            case SuppliesType.储物盒:
                for (let i = 0; i < this.data.Count; i++) {
                    let goodName = outGoods[ZTool.GetRandomInt(0, outGoods.length)];
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.战利品).find(e => e.Name == goodName));
                    data && (this.itemDatas.push(data));
                }
                break;

            case SuppliesType.电脑:
                for (let i = 0; i < this.data.Count; i++) {
                    let goodName = outGoods[ZTool.GetRandomInt(0, outGoods.length)];
                    data = ZTool.DeepCopy(DataManager.ItemDatas.get(ItemType.战利品).find(e => e.Name == goodName));
                    data && (this.itemDatas.push(data));
                }
                break;
        }

    }

    //敌人的死亡箱
    InitDieBox(name: string, itemDatas) {
        this.itemDatas = [];
        this.itemDatas = itemDatas;
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.data = ZTool.DeepCopy(DataManager.SuppliesDatas.get(SuppliesType.武器箱).find(e => e.Name == "小军火箱"));
        this.data.PlayerName = name;
        ResourceUtil.LoadSpriteFrame(`Supplies/${this.data.Type}_${this.data.Name}_0`).then((sf: cc.SpriteFrame) => {
            this.Icon.spriteFrame = sf;
        });

    }

    Take() {
        if (this.data && !this.data.Taken && this.data.HasOpenState) {
            this.data.Taken = true;
            ResourceUtil.LoadSpriteFrame(`Supplies/${this.data.Type}_${this.data.Name}_1`).then((sf: cc.SpriteFrame) => {
                this.Icon.spriteFrame = sf;
            });
        }
    }

}